/*
 * Copyright (C) 2024 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.wakala.employment.utils;

import android.content.Context;

import com.umeng.analytics.MobclickAgent;
import com.wakala.employment.activity.LoginActivity;
import com.wakala.employment.adapter.entity.LoginInfo;
import com.wakala.employment.adapter.entity.User;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.common.StringUtils;

/**
 * Token管理工具
 *
 * @author xuexiang
 * @since 2019-11-17 22:37
 */
public final class TokenUtils {

    private static String sToken;
    private static String mSign = "";

    /**
     * 当前登录的用户
     */
    private static User mLoginUser;

    private static final String KEY_TOKEN = "com.wakala.employment.utils.KEY_TOKEN";
    private static final String KEY_LOGIN_USER = "com.wakala.employment.utils.KEY_LOGIN_USER";
    private static final String KEY_SIGN = "com.wakala.employment.utils.KEY_SIGN";

    private TokenUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    private static final String KEY_PROFILE_CHANNEL = "github";

    /**
     * 初始化Token信息
     */
    public static void init(Context context) {
        MMKVUtils.init(context);
        sToken = MMKVUtils.getString(KEY_TOKEN, "");
        mLoginUser = MMKVUtils.getObject(KEY_LOGIN_USER, null);
    }

    public static void setToken(String token) {
        sToken = token;
        MMKVUtils.put(KEY_TOKEN, token);
    }

    public static void clearToken() {
        sToken = null;
        MMKVUtils.remove(KEY_TOKEN);
    }

    public static String getToken() {
        if(StringUtils.isEmpty(sToken)){
            sToken = MMKVUtils.getString(KEY_TOKEN, "");
        }
        return sToken;
    }

    public static boolean hasToken() {
        return MMKVUtils.containsKey(KEY_TOKEN);
    }

    /**
     * 处理登录成功的事件
     *
     * @param loginInfo 账户信息
     */
    public static boolean handleLoginSuccess(LoginInfo loginInfo) {
        if (!StringUtils.isEmpty(loginInfo.getToken())) {
            XToastUtils.success("登录成功！");
            MobclickAgent.onProfileSignIn(KEY_PROFILE_CHANNEL, loginInfo.getToken());
            setToken(loginInfo.getToken());
            return true;
        } else {
            XToastUtils.error("登录失败！");
            return false;
        }
    }

    /**
     * 处理登出的事件
     */
    public static void handleLogoutSuccess() {
        MobclickAgent.onProfileSignOff();
        //登出时，清除账号信息
        clearToken();
        clearSign();
        clearLoginUser();
        XToastUtils.success("登出成功！");
        SettingUtils.setIsAgreePrivacy(false);
        //跳转到登录页
        ActivityUtils.startActivity(LoginActivity.class);
    }

    public static String getmSign() {
        return mSign;
    }

    public static void setmSign(String mSign) {
        mSign = mSign;
        MMKVUtils.put(KEY_SIGN, mSign);
    }

    public static User getmLoginUser() {
        if(null==mLoginUser){
            //mLoginUser = MMKVUtils.getObject(KEY_LOGIN_USER,null);
            mLoginUser = MMKVUtils.getBeanObject(KEY_LOGIN_USER, User.class);
        }
        return mLoginUser;
    }

    public static void setmLoginUser(User mLoginUser) {
        mLoginUser = mLoginUser;
        //MMKVUtils.put(KEY_LOGIN_USER, mLoginUser);
        MMKVUtils.putBeanObject(KEY_LOGIN_USER,mLoginUser);
        System.out.println(MMKVUtils.getBeanObject(KEY_LOGIN_USER, User.class));
    }

    public static void clearSign() {
        mSign  = null;
        MMKVUtils.remove(KEY_SIGN);
    }

    public static void clearLoginUser(){
        mLoginUser = null;
        MMKVUtils.remove(KEY_LOGIN_USER);
    }
}
